package com.jeesite.modules.flow.web;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.flow.entity.ActIdGroup;
import com.jeesite.modules.flow.entity.ActIdUser;
import org.activiti.engine.IdentityService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.GroupQuery;
import org.activiti.engine.identity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 流程用户组Controller
 *
 * @author Xianlu Tech
 * @date 2019-10-02
 */
@Controller
@RequestMapping(value = "${adminPath}/flow/group")
public class ActIdGroupController extends BaseController {

    @Autowired
    private IdentityService identityService;

    /**
     * 查询列表
     */
    @RequestMapping(value = {"list", ""})
    public String list(ActIdGroup actIdGroup, Model model) {
        model.addAttribute("actIdGroup", actIdGroup);
        return "modules/flow/group/groupList";
    }

    /**
     * 查询流程用户组列表
     */
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<ActIdGroup> listData(ActIdGroup actIdGroup, HttpServletRequest request, HttpServletResponse response) {
        GroupQuery groupQuery = identityService.createGroupQuery();
        if (StringUtils.isNotBlank(actIdGroup.getId())) {
            groupQuery.groupId(actIdGroup.getId());
        }
        if (StringUtils.isNotBlank(actIdGroup.getName())) {
            groupQuery.groupNameLike("%" + actIdGroup.getName() + "%");
        }

        actIdGroup.setPage(new Page<>(request, response));
        List<Group> groupList = groupQuery.listPage((actIdGroup.getPage().getPageNo() - 1) * actIdGroup.getPage().getPageSize(), actIdGroup.getPage().getPageSize());

        Page<ActIdGroup> page = new Page<>();
        page.setPageNo(actIdGroup.getPage().getPageNo());
        page.setPageSize(actIdGroup.getPage().getPageSize());

        List<ActIdGroup> actIdGroupList = new ArrayList<>();
        for (Group group: groupList) {
            ActIdGroup idGroup = new ActIdGroup();
            idGroup.setId(group.getId());
            idGroup.setName(group.getName());
            actIdGroupList.add(idGroup);
        }
        page.setList(actIdGroupList);
        page.setCount(actIdGroupList.size());
        return page;
    }

}
